<template>
  <el-drawer direction="rtl" v-model="dialogVisible" :title="dialogTitle" size="70%">
    <!-- 搜索 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="租住历史">
          <el-switch
            v-model="showHistoryFlag"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
            @change="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="主键" align="center" prop="id" width="60" />
        <el-table-column label="租住地址" align="left" prop="rentAddrName" />
        <el-table-column label="租客姓名" align="center" prop="rentUserName" width="130" />
        <el-table-column label="租客手机号码" align="center" width="120">
          <template #default="scope">
            <span>{{ scope.row.rentUserResp.mobile }}</span>
          </template>
        </el-table-column>
        <el-table-column label="登记时间" align="center" prop="createTime" width="120">
          <template #default="scope">
            <span>{{ formatDate(scope.row.createTime, 'YYYY-MM-DD') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </el-drawer>
</template>
<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'
import * as RoomRentApi from '@/api/zwsj/roomRent'

defineOptions({ name: 'RoomRentsQuery' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const showHistoryFlag = ref(false)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  roomId: null
})

const queryFormRef = ref() // 搜索的表单

/** 打开弹窗 */
const open = async (roomId: number) => {
  dialogVisible.value = true
  dialogTitle.value = '房间内[当前]租住人员'
  showHistoryFlag.value = false

  //设置区域默认值
  queryParams.roomId = roomId
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    console.info(showHistoryFlag.value)
    if (showHistoryFlag.value) {
      const data = await RoomRentApi.getRoomHistoryRentPage(queryParams)
      list.value = data.list
      total.value = data.total
    } else {
      const data = await RoomRentApi.getRoomRentPage(queryParams)
      list.value = data.list
      total.value = data.total
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  if (true == showHistoryFlag.value) {
    dialogTitle.value = '房间内[历史]租住人员'
  } else {
    dialogTitle.value = '房间内[当前]租住人员'
  }
  queryParams.pageNo = 1
  getList()
}
</script>
